package com.yungu.swift.admin.route.system;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.admin.consts.AdminErrorEnum;
import com.yungu.swift.admin.utils.NetworkUtil;
import com.yungu.swift.admin.utils.RandomCipherUtils;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.model.PageVo;
import com.yungu.swift.system.sys.model.dto.SysUserDto;
import com.yungu.swift.system.sys.model.dto.SysUserOperateLogDto;
import com.yungu.swift.system.sys.model.param.AdminLevelPageParam;
import com.yungu.swift.system.sys.model.param.AdminUserAddParam;
import com.yungu.swift.system.sys.model.param.AdminUserParam;
import com.yungu.swift.system.sys.model.param.AdminUserResetPasswordParam;
import com.yungu.swift.system.sys.service.SysUserOperateLogService;
import com.yungu.swift.system.sys.service.SysUserService;
import com.yungu.swift.utils.Md5Utils;
import com.yungu.swift.utils.ParamUtil;
import com.yungu.swift.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;


/**
 * @author : cuixiuyin
 * @date : 2019/6/14
 */
@RestController
@RequestMapping("/admin/v1/system/permission/user")
@Api(value = "【系统服务-权限设置中心】", tags = "用户操作相关接口")
public class UserAdmin {

    @Reference
    private SysUserService sysUserService;
    @Reference
    private SysUserOperateLogService sysUserOperateLogService;

    @RequestMapping(value = "/queryPage", method = RequestMethod.POST)
    @ApiOperation(value = "用户信息分页查询", httpMethod = "POST")
    public ResponseData<PageVo<SysUserDto>> queryPage(@RequestBody AdminLevelPageParam pageParam) {
        return sysUserService.queryPage(pageParam);
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value = "用户信息新增", httpMethod = "POST")
    public ResponseData<Boolean> add(@RequestBody AdminUserAddParam userAddParam) {
        if (ParamUtil.isIllegal(userAddParam.getUserAccount(), userAddParam.getUserMobile())) {
            return ResponseData.buildErrorResponse(AdminErrorEnum.PARAM_ERROR.getCode(), AdminErrorEnum.PARAM_ERROR.getMsg());
        }
        return sysUserService.add(userAddParam);
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ApiOperation(value = "用户信息修改", httpMethod = "POST")
    public ResponseData<Boolean> edit(@RequestBody AdminUserParam adminUserParam, HttpServletRequest request) {
        // 解密参数
        if (StringUtils.isNotEmpty(adminUserParam.getPassword())) {
            String decrypt = RandomCipherUtils.randomDecrypt(adminUserParam.getPassword());
            adminUserParam.setPassword(Md5Utils.encode(decrypt));
        }
        SysUserDto editUserDto = new SysUserDto();
        BeanUtils.copyProperties(adminUserParam, editUserDto);
        editUserDto.setUpdateBy(adminUserParam.getUserUuid());
        ResponseData<Boolean> responseData = sysUserService.edit(editUserDto);
        if (responseData.isSuccess() && Objects.equals(responseData.getData(), Boolean.TRUE)) {
            final String ipAddress = NetworkUtil.getIpAddress(request);
            CompletableFuture.runAsync(() -> {
                SysUserOperateLogDto userOperateLogDto = new SysUserOperateLogDto();
                userOperateLogDto.setUuid(StringUtils.buildUUID());
                userOperateLogDto.setOperationIp(ipAddress);
                userOperateLogDto.setCreateOn(new Date());
                userOperateLogDto.setCreateBy(adminUserParam.getUserUuid());
                userOperateLogDto.setAppid(adminUserParam.getAppid());
                if (StringUtils.isNotEmpty(adminUserParam.getPassword())) {
                    userOperateLogDto.setUserUuid(adminUserParam.getUuid());
                    userOperateLogDto.setType(SysUserOperateLogDto.RESET_PASSWORD);
                    userOperateLogDto.setOperationRemark("重置用户密码-" + adminUserParam.getReason());
                }
                if (Objects.equals(adminUserParam.getStatus(), SysUserDto.STATUS_BLOCK)) {
                    userOperateLogDto.setUserUuid(adminUserParam.getUuid());
                    userOperateLogDto.setType(SysUserOperateLogDto.BLOCK);
                    userOperateLogDto.setOperationRemark("用户封号-" + adminUserParam.getReason());
                }
                if (Objects.equals(adminUserParam.getStatus(), SysUserDto.STATUS_NORMAL)) {
                    userOperateLogDto.setUserUuid(adminUserParam.getUuid());
                    userOperateLogDto.setType(SysUserOperateLogDto.UN_BLOCK);
                    userOperateLogDto.setOperationRemark("用户解封-" + adminUserParam.getReason());
                }
                sysUserOperateLogService.add(userOperateLogDto);
            });
        }
        return responseData;
    }


    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ApiOperation(value = "用户信息查询", httpMethod = "POST")
    public ResponseData<SysUserDto> list(@RequestBody AdminUserParam adminUserParam) {
        return sysUserService.get(adminUserParam.getUuid());
    }


    @RequestMapping(value = "/resetPassword", method = RequestMethod.POST)
    @ApiOperation(value = "用户信息逾期重置密码", httpMethod = "POST")
    public ResponseData<Boolean> resetPasswordExpired(@RequestBody AdminUserResetPasswordParam resetPasswordParam, HttpServletRequest request) {
        if (ParamUtil.isIllegal(resetPasswordParam.getUserAccount(), resetPasswordParam.getOldPassword(),
                resetPasswordParam.getNewPassword())) {
            return ResponseData.buildErrorResponse(AdminErrorEnum.PARAM_ERROR.getCode(), AdminErrorEnum.PARAM_ERROR.getMsg());
        }
        resetPasswordParam.setUserAccount(RandomCipherUtils.randomDecrypt(resetPasswordParam.getUserAccount()));
        resetPasswordParam.setOldPassword(RandomCipherUtils.randomDecrypt(resetPasswordParam.getOldPassword()));
        resetPasswordParam.setNewPassword(RandomCipherUtils.randomDecrypt(resetPasswordParam.getNewPassword()));
        ResponseData<Boolean> responseData = sysUserService.resetPassword(resetPasswordParam);
        if (responseData.isSuccess() && Objects.equals(responseData.getData(), Boolean.TRUE)) {
            final String ipAddress = NetworkUtil.getIpAddress(request);
            CompletableFuture.runAsync(() -> {
                SysUserOperateLogDto userOperateLogDto = new SysUserOperateLogDto();
                userOperateLogDto.setUuid(StringUtils.buildUUID());
                userOperateLogDto.setUserUuid(resetPasswordParam.getUserUuid());
                userOperateLogDto.setAppid(resetPasswordParam.getAppid());
                userOperateLogDto.setType(SysUserOperateLogDto.RESET_PASSWORD);
                userOperateLogDto.setOperationRemark("用户信息逾期重置密码");
                userOperateLogDto.setOperationIp(ipAddress);
                userOperateLogDto.setCreateOn(new Date());
                userOperateLogDto.setCreateBy(resetPasswordParam.getUserUuid());
                sysUserOperateLogService.add(userOperateLogDto);
            });
        }
        return responseData;
    }
}
